package com.ln.lnzw.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.ImageView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.ln.lnzw.R;
import com.ln.lnzw.adapter.NewsFragmentPagerAdapter;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.TabEntity;
import com.ln.lnzw.fragment.ConsultationReplyFragment;
import com.ln.lnzw.fragment.MyConsultFragment;
import com.ln.lnzw.view.CustomViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wuwei on 2018/5/9.
 * 咨询回复
 */

public class ConsultationReplyActivity extends BaseActivity {
    @BindView(R.id.viewpager)
    CustomViewPager viewpager;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.commonTabLayout)
    CommonTabLayout commonTabLayout;
    private String[] mTitles = {"咨询回复", "我要咨询"};
    private int[] mIconUnselectIds = {R.mipmap.icon_banshi_zixunhuifu_unselect
            , R.mipmap.icon_banshi_woyaozixun_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.icon_banshi_zixunhuifu_select, R.mipmap.icon_banshi_woyaozixun_select};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consultation_reply);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
            mFragments.add(new ConsultationReplyFragment());
            mFragments.add(new MyConsultFragment());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }

    private void initView() {
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }


}
